package com.linghu.sportplants.utils;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.linghu.sportplants.dao.DatabaseLaunch;
import com.linghu.sportplants.entity.ConstantFields;


/**
 * @Author : LingHu
 * @Email : 1714873054@qq.com
 * @Date : on 2023/10/4 16:37.
 * @Description :描述
 */
public class SqlPlantUtils {
    private  final DatabaseLaunch conn;//连接
    private static SQLiteDatabase db = null;//数据库
    private String plant_name;

    public SqlPlantUtils(Context context) {
        conn=new DatabaseLaunch(context);
        db = conn.getWritableDatabase();//初始化数据库
    }

    //关闭数据库
    public static void closeSql(){
        db.close();
    }

    //增删改查
    public void insert(/**Integer id,String name,Integer age,Integer salary*/){
        String sql="insert into "+ConstantFields.TABLE_NAME+" ( id,step_count,plant_name) values(?,?,?);";
//                "insert into "+ ConstantFields.TABLE_NAME+"(id,name,age,salary) values(?,?,?,?)";
//        db.execSQL(sql,new Object[]{id,name,age,salary});
        db.execSQL(sql,new Object[]{1,10,"蒲公英"});
        db.close();
    }
    //删除
    public void delete(Integer id){
        String sql=
                "delete from "+ConstantFields.TABLE_NAME+" where id=id";
        db.execSQL(sql);
    }

    public String query(){
        String sql=
                "SELECT plant_name FROM " +ConstantFields.TABLE_NAME+" WHERE step_count = 10";
        Cursor cursor = db.rawQuery(sql, null);
        while (cursor.moveToNext()){
            int index = cursor.getColumnIndex("plant_name");
            plant_name = cursor.getString(index);
            return plant_name;
        }
        cursor.close();
        db.close();
        return plant_name;
    }

    public void update(Integer salaries,Integer ages){
       String sql=
                "update "+ConstantFields.TABLE_NAME+" set salary="+salaries+ " where age="+ages;
        db.execSQL(sql);
    }

}
